package com.weblog.user.dao;

import com.weblog.model.user.Role;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface RoleDao extends JpaRepository<Role,String>, JpaSpecificationExecutor<Role> {


    @Query(value = "select *from tb_role where rid in(select rid from tb_ur where userid =?1",nativeQuery = true)
    List<Role> findByUserid(String userid);

    /**
     * 根据角色名查询角色
     * @param rname
     * @return
     */
    Role findRoleByRname(String rname);

    /**
     * 根据id查询是否为系统角色
     * @param rid
     * @return
     */
    @Query(value = "SELECT issystemrole FROM tb_role WHERE rid =?1 ",nativeQuery = true)
    String findIssystemroleByRid(String rid);

    /**
     * 修改角色状态
     * @param rid
     */
    @Modifying
    @Query(value = "UPDATE tb_role SET state =?2 WHERE rid = ?1",nativeQuery = true)
    void changeState(String rid, String state);

    /**
     * 给角色添加用户
     * @param rid
     * @param aid
     */
    @Modifying
    @Query(value = "insert into tb_ur SET aid =?2,rid =?1",nativeQuery = true)
    void addAdminToRole(String rid, String aid);

    /**
     * 给角色添加权限
     * @param rid
     * @param pid
     */
    @Modifying
    @Query(value = "insert into tb_rp SET rid =?1,pid =?2",nativeQuery = true)
    void addPermissionToRole(String rid, String pid);

}
